﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using com.dz.common;
using com.dz.entity;
using com.dz.httpmoudle;
using com.dz.components;
namespace com.dz.web.admin
{
    public partial class Login : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string userName = Request["userName"];
            string password = Request["userPassword"];
            string vfCode = Request["verifyCode"];

            //过滤关键字
            userName =SystemUtil.MSGFilterImport(userName);
            password = SystemUtil.MSGFilterImport(password);
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(vfCode))
            {
                lblErrorInfo.Text = "请输入完整的登陆信息";
                return;
            }

            string cookieVfCode = CookieManager.GetVifCode();
            if (string.IsNullOrEmpty(cookieVfCode))
            {
                LogHelper.Error("获取用户登陆验证码信息为空");
                lblErrorInfo.Text = "验证码信息未生成,请稍后重试";
                return;

            }

            //验证验证码
            string[] cookieVfArr = cookieVfCode.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
            if (cookieVfArr == null || cookieVfArr.Length != 3)
            {
                lblErrorInfo.Text = "验证码格式信息有误";
                return;
            }

            //验证时间
            string vfCodeDt = cookieVfArr[0].ToString();
            DateTime dt = DateTime.Now;
            if (!DateTime.TryParse(vfCodeDt, out dt))
                dt = DateTime.MinValue;

            TimeSpan timeSpan = DateTime.Now.Subtract(dt);
            if (timeSpan.Seconds > 60)
            {
                lblErrorInfo.Text = "验证码已过期";
                return;
            }

            //验证验证码信息
            if (string.Compare(vfCode, cookieVfArr[1].ToString(), true) != 0)
            {
                lblErrorInfo.Text = "验证码错误";
                return;
            }

            if (string.Compare(SystemUtil.MD5Mac(vfCode + Consts.VifCode), cookieVfArr[2].ToString(), true) != 0)
            {
                lblErrorInfo.Text = "验证码信息错误";
                return;
            }

            if (userName.Equals("admin_dzxs") && password.Equals("admin_2004111091"))
            {
                UserToken token = new UserToken(1, "admin", "管理员");
                UserLoginHelper.SetUserTicket("admin", token.ToString());
                Response.Redirect("/admin/main.aspx", true);

            }
            else
            {
                LogHelper.Info("有人试图登录后台" + DateTime.Now.ToString());
            }

            //UserManager userMgr = new UserManager();
            //try
            //{
            //   BizResult<int> biz= userMgr.LoginUser(userName, password, vfCode);
            //   if (biz != null)
            //   {
            //       if (!biz.IsSuccess)
            //       {
            //           lblErrorInfo.Text = biz.ReturnString;
            //       }
            //       else
            //       {
            //           UserInfo userInfo = userMgr.GetUserInfoByUserName(userName);
            //           if (userInfo != null)
            //           {
            //               UserToken token = new UserToken(userInfo.UserId, userInfo.UserName, userInfo.UserRoleString, userInfo.UserRealName, userInfo.UserRoleId, userInfo.DepartId);
            //               UserLoginHelper.SetUserTicket(userInfo.UserId.ToString(), token.ToString());
            //           }
            //           Response.Redirect("/admin/Default.aspx", true);
            //       }
            //   }
            //}
            //catch (Exception ex)
            //{
            //    LogHelper.Error("用户登陆失败" + ex.ToString());
            //    lblErrorInfo.Text = "登陆过程出现异常,请稍后重试";
            //}
        }
    }
}
